package my.jwds.p1995;

import java.util.*;

public class CountQuadruplets {

    public static void main(String[] args) {
        CountQuadruplets obj = new CountQuadruplets();
        int [] nums = new int[]{23,3,76,10,21,50,34,30,82,58,30,71,56,49,12,90,2,53};
        System.out.println(obj.countQuadruplets(nums));
        System.out.println(obj.countQuadruplets1(nums));
    }

    public int countQuadruplets(int[] nums) {

        int res = 0,len = nums.length;
        Map<Integer,Integer> set = new HashMap<>();
        for (int i = 0; i < len; i++) {
            set.put(nums[i],i);
        }
        for (int i = 0; i < len; i++) {
            for (int j = i+1; j < len; j++) {
                for (int k = j+1; k < len; k++) {
                    int sum = nums[i]+nums[j]+nums[k];
                    if (set.get(sum) != null && set.get(sum)> k){
                        System.out.println(Arrays.toString(new int[]{i,j,k,set.get(sum)})+Arrays.toString(new int[]{nums[i],nums[j],nums[k],sum}));
                        res++;
                    }
                }
            }
        }
        return res;
    }
    public int countQuadruplets1(int[] nums) {

        int res = 0,len = nums.length;
        for (int i = 0; i < len; i++) {
            for (int j = i+1; j < len; j++) {
                Map<Integer,Integer> set = new HashMap<>();
                for (int k = j+1; k < len; k++) {
                    if (set.containsKey(nums[k])){
                        res+=set.get(nums[k]);
                    }
                    int sum = nums[i]+nums[j]+nums[k];
                    set.putIfAbsent(sum,0);
                    set.put(sum,set.get(sum)+1);
                }
            }
        }
        return res;
    }

    public int countQuadruplets2(int[] nums) {

        int res = 0,len = nums.length;
        for (int i = 0; i < len; i++) {
            for (int j = i+1; j < len; j++) {
                
            }
        }
        return res;
    }
}
